package com.dgut.courses_selecting_sys.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.dgut.courses_selecting_sys.constants.RabbitMqKey;
import com.dgut.courses_selecting_sys.dto.MQUserActivityDto;
import com.dgut.courses_selecting_sys.entity.UserActivities;
import com.dgut.courses_selecting_sys.mapper.UserActivitiesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(queuesToDeclare = @Queue(value = RabbitMqKey.ACTIVITY_USER_CONSUMER,durable = "true"))
public class ActivityUserConsumer {

    @Autowired
    UserActivitiesMapper userActivitiesMapper;

    @RabbitHandler
    public void receive(String message){

        MQUserActivityDto jsonObject = JSON.parseObject(message,MQUserActivityDto.class);
        UserActivities userActivities =new UserActivities();
        userActivities.setUserId(jsonObject.getUserId());
        userActivities.setActivityId(jsonObject.getActivityId());
        userActivitiesMapper.insert(userActivities);
        log.info("用户选课日志："+userActivities);


    }

}